// GoView.cpp : implementation file
//

#include "stdafx.h"
#include "MainMenuView.h"
#include "StaticTextWidget.h"
#include ".\mainmenuview.h"


// CGoView

IMPLEMENT_DYNCREATE(CMainMenuView, CViewWnd)





BEGIN_MESSAGE_MAP(CMainMenuView, CViewWnd)

	ON_MESSAGE(WM_EVENT_WRAPPER_FOR_THREAD_SAFE,HandleModelEvent)
	ON_WM_ACTIVATE()
	ON_WM_SIZE()

	ON_WM_PAINT()

END_MESSAGE_MAP()


// CGoView drawing

void CMainMenuView::OnDraw(CDC* pDC)
{

	this->m_pRootContainer->InvalidateContent(0);
	// TODO: add draw code here
}


// CGoView diagnostics

#ifdef _DEBUG
void CMainMenuView::AssertValid() const
{
	CWnd::AssertValid();
}

void CMainMenuView::Dump(CDumpContext& dc) const
{
	CWnd::Dump(dc);
}
#endif //_DEBUG



void CMainMenuView::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
	__super::OnActivate(nState, pWndOther, bMinimized);

	// TODO: Add your message handler code here
}

void CMainMenuView::OnSize(UINT nType, int cx, int cy)
{
	__super::OnSize(nType, cx, cy);


}


void CMainMenuView::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	// Do not call __super::OnPaint() for painting messages
	this->m_pRootContainer->GetDisplayManage()->SetDisplayReady(true);
	this->m_pRootContainer->InvalidateContent(0);
}
	 LRESULT CMainMenuView::HandleModelEvent(WPARAM pModeEvent,LPARAM dummy)
	{
		ModelEvent* pEvent=(ModelEvent*)pModeEvent;
		unsigned int evCode = pEvent->evCode;

		if(evCode==WIDGET_LIST_ITEM_SELECTED)
		{
				CWidget* pSelectedBarItem=this->m_pBarWidget->GetSelectedItem();
				if(pSelectedBarItem!=NULL)
				{
					if(pSelectedBarItem->GetPayLoad()==1)
					{

							if(m_pGoView==NULL)
							{
								m_pGoView=new CGoView();
								//m_pGoView->EnableBmpCache(true);
								m_pGoView->GetEventModel()->AddListener(this->GetEventListener());
								this->m_pViewManager->AddView(m_pGoView,
									pSelectedBarItem->GetPayLoad());
								
														
							}

							WRect rc;
							this->m_pRootContainer->GetRect(&rc);
							rc.x=0;
							rc.y=0;
							rc.dy-=130;
							m_pGoView->SetRect(&rc);
							m_pGoView->SetVisible(false);
							m_pGoView->StartGameOffline(WEIQI_SIDE_BLACK,19);

							

							m_pViewManager->SwitchView(m_pGoView);
					}
					if(pSelectedBarItem->GetPayLoad()==2)
					{

						if(m_pListView==NULL)
						{
							m_pListView=new CViewContainer();
								this->m_pViewManager->AddView(m_pListView,
									pSelectedBarItem->GetPayLoad());
							WRect rc;
							this->m_pRootContainer->GetRect(&rc);
							rc.x=0;
							rc.y=0;
							rc.dy-=130;
							m_pListView->SetRect(&rc);
							this->TestListWidget();
							
						}
						m_pViewManager->SwitchView(m_pListView);
						
					}

					if(pSelectedBarItem->GetPayLoad()==3)
					{




					}
					if(pSelectedBarItem->GetPayLoad()==4)
					{
						WPoint pt_orig;
						pt_orig.x=150;
						pt_orig.y=150;
						pt_orig.z=-100;
						this->m_pTestWidget->RotateY(&pt_orig,20);
						this->m_pTestWidget->InvalidateContent(0);
					}

					if(pSelectedBarItem->GetPayLoad()==5)
					{
						CAnimateThread* pThread = CreateNewAnimateThread(this->GetEventListener());
						C3DRotateWidgetAnimateParam* animate_param=new C3DRotateWidgetAnimateParam();
						WRect last_rc;
						m_pTestWidget->GetRect(&last_rc);
					
						float widget_z=0;
						animate_param->start_radian =0;

						
						float z_speed=abs(widget_z);
						float r_speed=1;


							animate_param->max_radian  =-45;
							animate_param->nAcceler=0;
							animate_param->nSpeed=-r_speed;
							animate_param->nOrigX = 0;

					
						animate_param->nOrigY=last_rc.dy/2;

						animate_param->start_z=0;
						animate_param->max_z = 0;

						animate_param->z_speed=-z_speed;
						animate_param->z_acceler =0;

						
						animate_param->nFadeOption=0;
						animate_param->pSurface=NULL;
						animate_param->pThread=pThread;
						animate_param->pWidget=m_pTestWidget;
						AddNewFrame(animate_param);
						StartThread(pThread);
					}

				}

		}
		delete pEvent;

		return 0;
	}
void CMainMenuView::TestGridMenu()
{
		m_pMainMenu= new CGridMenuContainer();
		this->m_pRootContainer->InsertWidget(m_pMainMenu);	
		WRect rc;
		this->m_pRootContainer->GetRect(&rc);
		rc.x=0;
		rc.y=0;
		m_pMainMenu->SetRect(&rc);
		m_pMainMenu->SetGridProperty(4,4,64,64);
		CMenuProperty menPro;
		menPro.icon_h=64;
		menPro.icon_w=64;
		menPro.x=1;
		menPro.y=1;
		menPro.onMenuSelected=NULL;
		strcpy(menPro.image,"img/weiqi_icon.bmp");
		m_pMainMenu->AddMenu(&menPro);

		menPro.x=2;
		menPro.y=1;
		m_pMainMenu->AddMenu(&menPro);
		menPro.x=3;
		menPro.y=1;
		m_pMainMenu->AddMenu(&menPro);
		menPro.x=4;
		menPro.y=1;
		m_pMainMenu->AddMenu(&menPro);
		menPro.x=1;
		menPro.y=2;
		m_pMainMenu->AddMenu(&menPro);
		
		menPro.x=2;
		menPro.y=2;
		m_pMainMenu->AddMenu(&menPro);
		menPro.x=3;
		menPro.y=2;
		m_pMainMenu->AddMenu(&menPro);
		menPro.x=4;
		menPro.y=2;
		m_pMainMenu->AddMenu(&menPro);

		menPro.x=1;
		menPro.y=3;
		m_pMainMenu->AddMenu(&menPro);
		menPro.x=2;
		menPro.y=3;
		m_pMainMenu->AddMenu(&menPro);
		menPro.x=3;
		menPro.y=3;
		m_pMainMenu->AddMenu(&menPro);
		menPro.x=4;
		menPro.y=3;
		m_pMainMenu->AddMenu(&menPro);		
		this->m_pMainMenu->SetBkGround("img/mainmenu_bk.bmp",470,470);
		
}


void CMainMenuView::TestListWidget()
{
	int nItems=4;
	m_pListWidget = new CListWidget();
	CListWidget* pListWidget=m_pListWidget;
	this->m_pListView->InsertWidget(pListWidget);	
	m_pListView->AddGarbage(pListWidget);
	WRect rc;
	this->m_pListView->GetRect(&rc);
	pListWidget->SetBackgroundColor(RGB(216,233,226));
	rc.x=4;
	rc.y=4;
	rc.dx-=8;
	rc.dy=30*6;
	pListWidget->SetRect(&rc);


#if(0)
	char draw_cmd[128];
	CImageWidget* pImageWidget = NULL;
	rc.dy=40;
	WRect rcW;


	for(int i=0;i<6;i++)
	{
		pImageWidget = new CImageWidget();
		pImageWidget->SetRect(&rc);
		pListWidget->AddItem(pImageWidget,-1);
		pImageWidget->GetRect(&rcW);
		sprintf(draw_cmd,"H:1,0,%d,%d,%d,1,",40*i,rcW.dx,rcW.dy);
		pImageWidget->setImage("img/new.bmp",draw_cmd);

		if(i%2==0)
			pImageWidget->SetAlpha(100);


	}
#else

	char* format ="<font color=#ff0000>This is Red Text</font>, <b>This is Bold Text</b>, \
This harmless looking string will actually <u><b>wrap</b></u>line (%d)";
	char* format2 = "This is test item (%d)";
	CString str;
	for(int i=0;i<nItems;i++)
	{
		CStaticTextWidget* txtWidget=new CStaticTextWidget();
		str.Format(format2,i);
		txtWidget->SetText(str);
		rc.dy=30;
		txtWidget->SetRect(&rc);
		pListWidget->AddItem(txtWidget);
		txtWidget->SetBackgroundColor(RGB(216,233,226));
		if(i%2==1)
		{
		//	txtWidget->SetBackgroundColor(0xededed);
		}

		pListWidget->AddGarbage(txtWidget);




	}

	this->m_pCurrentWidget=this->m_pListWidget;
	this->m_pListWidget->InvalidateContent(0);

#endif


}

void CMainMenuView::Test3DListWidget()
{
	int nItems=24;
	m_p3DListWidget = new C3DListWidget();
	m_p3DListWidget->GetEventModel()->AddListener(this->GetEventListener());
	m_p3DListWidget->SetBackgroundColor(RGB(0,0,0));
	
	this->m_pRootContainer->InsertWidget(m_p3DListWidget);	
	this->m_pRootContainer->AddGarbage(m_p3DListWidget);
	WRect rc;
	this->m_pRootContainer->GetRect(&rc);
	rc.x=0;
	rc.y=50;
	rc.dy=400;
	m_p3DListWidget->SetRect(&rc);

	

	char draw_cmd[128];
	char img_fn[128];
	CImageWidget* pImageWidget = NULL;
	
	rc.dy=247;
	rc.dx=176;
	//rc.dy=100;
	//rc.dx=100;
	WRect rcW;
	//pBarWidget->m_nItemAngle = 40;
	//pBarWidget->m_nSeparateDx = 2;

	for(int i=0;i<nItems;i++)
	{
		pImageWidget = new CImageWidget();
		pImageWidget->SetRect(&rc);
		m_p3DListWidget->AddItem(pImageWidget,-1);
		pImageWidget->GetRect(&rcW);
		sprintf(draw_cmd,"H:1,0,0,%d,%d,1,",rcW.dx,rcW.dy);
		sprintf(img_fn,"resource/china%d.bmp",i%12+1);
		pImageWidget->setImage(img_fn,draw_cmd);
		pImageWidget->SetPayLoad(i+1);
		m_p3DListWidget->AddGarbage(pImageWidget);
		if(i==6)
		{
			m_p3DListWidget->SetFocusItem(pImageWidget);
		}
	}

	//pBarWidget->Scroll(300,0);

	//pBarWidget->SetBackgroundColor(RGB(216,233,226));
	m_p3DListWidget->ReCalLayout(true);

}

void CMainMenuView::TestBarWidget()
{
	int nItems=8;
	CBarWidget* pBarWidget = new CBarWidget();
	m_pBarWidget=pBarWidget;
	this->m_pBarWidget->GetEventModel()->AddListener(this->GetEventListener());
	//pBarWidget->SetBackgroundColor(RGB(216,233,226));
	pBarWidget->SetBackgroundColor(RGB(0,0,0));
	
	this->m_pRootContainer->InsertWidget(pBarWidget);	
	WRect rc;
	this->m_pRootContainer->GetRect(&rc);
	rc.x=0;
	rc.y=rc.dy-100;
	rc.dy=150;
	pBarWidget->SetRect(&rc);

	

	char draw_cmd[128];
	char img_fn[128];
	CImageWidget* pImageWidget = NULL;
	rc.dy=48;
	rc.dx=48;
	WRect rcW;
	for(int i=0;i<nItems;i++)
	{
		pImageWidget = new CImageWidget();
		pImageWidget->SetRect(&rc);
		pBarWidget->AddItem(pImageWidget,-1);
		pImageWidget->GetRect(&rcW);
		sprintf(draw_cmd,"H:1,0,0,%d,%d,1,",rcW.dx,rcW.dy);
		sprintf(img_fn,"resource/iphone%d.bmp",i+1);
		pImageWidget->setImage(img_fn,draw_cmd);
		pImageWidget->SetPayLoad(i+1);
		pBarWidget->AddGarbage(pImageWidget);
	}



}

void CMainMenuView::OnTest2()
{
	

		CMemoryFrameSurface* pSurface = NULL;
		
		CAnimateThread* pThread = NULL;
		m_pCurrentWidget->SetVisible(false);
		m_pCurrentWidget->InvalidateContent(0);
		

		CFlipAnimateParam* move_param=NULL;
		if(1)
		{
			move_param= new CFlipAnimateParam();
		
			pThread= CreateNewAnimateThread(&(this->m_animateListener));

			pSurface = m_pCurrentWidget->CreateMemoryFrameSurface();
			
			move_param->max_radian=18000;
			move_param->start_radian=0;
			move_param->view_z=140*5;
			move_param->flip_type=FLIP_HORI;
			move_param->nAcceler=0;
			move_param->nFadeOption=0;
			move_param->nSpeed=3;
			move_param->pSurface=pSurface;
			move_param->pThread=pThread;
			move_param->pWidget=NULL;
			move_param->bUpdateScreen=false;
			AddNewFrame(move_param);


			StartThread(pThread);
			}

		 CWidget* m_pGhost=m_pCurrentWidget->GetGhost();
		 if(m_pGhost!=NULL)
		 {
			m_pGhost->SetVisible(false);
			m_pGhost->InvalidateContent(0);
			pSurface =m_pGhost->CreateMemoryFrameSurface();
			pThread = CreateNewAnimateThread(&(this->m_animateListener));

			
			move_param=new CFlipAnimateParam();
			move_param->max_radian=18000;
			move_param->start_radian=0;
			move_param->view_z=140*5;
			move_param->flip_type=FLIP_HORI;
			move_param->nAcceler=0;
			move_param->nFadeOption=0;
			move_param->nSpeed=3;
			move_param->pSurface=pSurface;
			move_param->pThread=pThread;
			move_param->pWidget=NULL;
			move_param->bUpdateScreen=true;
			AddNewFrame(move_param);


			StartThread(pThread);
		 }


		
}
void CMainMenuView::OnTest1()
{
	/*
	CMemoryFrameSurface* pSurface = this->m_pListWidget->CreateMemoryFrameSurface();
	pSurface->Aggregate();
	pSurface->m_pSurfaceMemBitmap->m_pBmp->TractrixX(42,82);
	pSurface->m_pSurfaceMemBitmap->m_pCDC->SelectObject((HGDIOBJ)NULL);
	pSurface->m_pSurfaceMemBitmap->m_pCDC->SelectObject(pSurface->m_pSurfaceMemBitmap->m_pBmp);
	pSurface->SetPainFinish(true);
	DisplayAddSurface(pSurface);
	*/
	m_pListWidget->RemoveItem(m_pListWidget->GetSelectedItem());



}
void CMainMenuView::OnTest3()
{

	m_pListWidget->Scroll(0,30);

}

void CMainMenuView::TestGhostWidget()
{

	WRect rc;


	CImageWidget* pImage = new CImageWidget();
	this->m_pRootContainer->InsertWidget(pImage);
	pImage->EnableMouseMove(true);
	
	rc.x=0;
	rc.y=0;
	rc.dx=113;
	rc.dy=113;
	pImage->SetRect(&rc);
	char draw_cmd[256];
	sprintf(draw_cmd,"H:1,0,0,113,113,1,SELF_MASK,");
	pImage->setImage("resource/fox.bmp",draw_cmd);
	

	CGhostWidget* ghost=new CGhostWidget();
	ghost->AttachWidget(pImage);
	this->m_pRootContainer->InsertWidget(ghost);
	rc.y+=rc.dy;
	ghost->SetRect(&rc);
	



	m_pRootContainer->InvalidateContent();

	m_pCurrentWidget=pImage;
}
void CMainMenuView::RefreshUI()
	{
		CRect wrc;
		GetWindowRect(&wrc);
		m_pCurrentWidget=NULL;
		this->m_pDisplayManage=new CDisplayManage(this->m_hWnd,wrc.Width(),wrc.Height());
		this->m_pRootContainer=new CContainerWidget();
		m_pDisplayManage->RegisterRootContainer(m_pRootContainer);
		m_pRootContainer->SetDisplayManage(this->m_pDisplayManage);

		m_pViewManager=new CViewManager(this->m_pRootContainer);
		WRect rootRc;
		CRECT_TO_WRECT(wrc,rootRc);
		rootRc.x=0;
		rootRc.y=0;
		m_pRootContainer->SetRect(&rootRc,false);
		//add a background widget for root container
	
		CBackGroundWidget* pBkW=new CBackGroundWidget();
		WRect rc;
		m_pRootContainer->GetRect(&rc);
		rc.x=0;
		rc.y=0;
		m_pRootContainer->InsertWidget(pBkW,Z_ORDER_BACKGROUND);
		pBkW->SetRect(&rc);
		pBkW->SetBackgroundColor(RGB(0,0,0));
		m_pRootContainer->AddGarbage(pBkW);
		

		//add a image widget, and animate moving it
#if(0)	
		{

			CWeiQiPaneWidget* pane = new CWeiQiPaneWidget();
			pane->init(19,12,12);
			m_pRootContainer->InsertWidget(pane);
			char* drawcmd = "H:1,0,0,470,470,1,";
			pane->setImage("resource/weiqipan.bmp",drawcmd);
			WRect rc;
			rc.dx=19*24+24*2;
			rc.dy=19*24+24*2;
			rc.x=500;
			rc.y=0;		
			pane->SetRect(&rc);
			
			CImageWidget* pImageWidget=new CImageWidget();
			char draw_cmd[128];
			char img_fn[128];
			sprintf(draw_cmd,"H:1,0,0,%d,%d,1,",48,48);
			sprintf(img_fn,"resource/iphone%d.bmp",1);
			pImageWidget->setImage(img_fn,draw_cmd);
			m_pRootContainer->InsertWidget(pImageWidget);
			
			pImageWidget->SetRect(1000,0,48,48);

			WRect rcScreen;
			pImageWidget->GetRectAsScreen(&rcScreen);

			CAnimateThread* pThread = CreateNewAnimateThread(this->GetEventListener());					
			pThread = CreateNewAnimateThread(this->GetEventListener());
			ANIMATE_LINE_MOVE_WIDGET_PARAM_T* move_param=
				(ANIMATE_LINE_MOVE_WIDGET_PARAM_T*)malloc(sizeof(ANIMATE_LINE_MOVE_WIDGET_PARAM_T));				
			move_param->x1=rcScreen.x;
			move_param->y1=rcScreen.y;
			move_param->x2=0;
			move_param->y2=rcScreen.y;
			move_param->nAcceler=0;
			move_param->nFadeOption=0;
			move_param->nSpeed=1;
			move_param->pSurface=NULL;
			move_param->pThread=pThread;
			move_param->pWidget=pane;
			AddLineMoveWidgetFrame(move_param);

			StartThread(pThread);
		}
#endif
	
#if(0)
		{
			if(m_pGoView==NULL)
			{
				m_pGoView=new CGoView();
				m_pGoView->GetEventModel()->AddListener(this->GetEventListener());

				this->m_pRootContainer->InsertWidget(m_pGoView);
				
										
			}

			WRect rc;
			this->m_pRootContainer->GetRect(&rc);
			rc.x=550;
			rc.y=0;
			rc.dy-=130;
			m_pGoView->SetRect(&rc);
			m_pGoView->SetVisible(false);
			m_pGoView->StartGameOffline(WEIQI_SIDE_BLACK,19);
			m_pGoView->SetVisible(true);
		}



#endif


#if(0)
		{
			this->m_pTestWidget = new CContainerWidget();
			this->m_pRootContainer->InsertWidget(this->m_pTestWidget);
			m_pTestWidget->SetRect(50,50,300,300);
			CImageWidget* pImage=new CImageWidget();
			char draw_cmd[128];
			char img_fn[128];
			sprintf(draw_cmd,"H:1,0,0,%d,%d,1,",300,300);
			sprintf(img_fn,"resource/test.bmp");
			((CImageWidget*)pImage)->setImage(img_fn,draw_cmd);
			((CContainerWidget*)m_pTestWidget)->InsertWidget(pImage);
			pImage->SetRect(0,0,300,300);
			m_pTestWidget->m_fViewZ=600;



			/*
			CGhostWidget* ghost=new CGhostWidget();
			ghost->AttachWidget(m_pTestWidget);
			this->m_pRootContainer->InsertWidget(ghost);
			*/
		}
#endif

#if(1)
		Test3DListWidget();
#endif


#if(0)
		{
			WRect rc;
			this->m_pRootContainer->GetRect(&rc);
			rc.x=0;
			rc.y=100;
			rc.dy=50;
			

			CString str;
			CStaticTextWidget* txtWidget=new CStaticTextWidget();
			str="Hello easyGUI!";
			txtWidget->SetText(str);
			txtWidget->SetRect(&rc);
			m_pRootContainer->InsertWidget(txtWidget);
			txtWidget->SetBackgroundColor(RGB(216,233,226));
			txtWidget->InvalidateContent();
		}
#endif

		//TestListWidget();
		//this->m_pListWidget->SetVisible(FALSE);
		//TestGhostWidget();
		TestBarWidget();

		
//		m_pRootContainer->InvalidateContent(0);

		

	}

	BOOL CMainMenuView::PreTranslateMessage(MSG* pMsg)
	{
		// TODO: Add your specialized code here and/or call the base class
		if(pMsg->message == WM_KEYDOWN )
		{
			//(wParam), LOWORD(lParam), HIWORD(lParam)
			WKEY_EVENT_T key_evt;
			key_evt.key_code=pMsg->wParam;;
			key_evt.rpt_cnt=LOWORD(pMsg->lParam);
			key_evt.flag=HIWORD(pMsg->wParam);			
			m_pRootContainer->HandleEvent(KEY_EVENT_DOWN,(int)(&key_evt),0);		
		}

		if( pMsg->message ==WM_KEYUP)
		{
			WKEY_EVENT_T key_evt;
			key_evt.key_code=pMsg->wParam;;
			key_evt.rpt_cnt=LOWORD(pMsg->lParam);
			key_evt.flag=HIWORD(pMsg->wParam);
			m_pRootContainer->HandleEvent(KEY_EVENT_UP,(int)(&key_evt),0);		
		}

		if(pMsg->message==WM_LBUTTONUP)
		{
			WPoint ptScreen;
			CPoint pt;
			pt.x=pMsg->pt.x;
			pt.y=pMsg->pt.y;
			this->ScreenToClient(&pt);
			ptScreen.x=pt.x;
			ptScreen.y=pt.y;
			m_pRootContainer->HandleEvent(MOUSE_LBUTTON_UP,(int)(&ptScreen),pMsg->lParam);		
#if(0)
			if(this->m_pGoView!=NULL)
			{
				WRect rc;
				this->m_pGoView->GetRect(&rc);
				rc.x=0;
				this->m_pGoView->SetRect(&rc);
				this->m_pGoView->InvalidateContent(0);
			}
#endif
		}

		
		if(pMsg->message==WM_LBUTTONDOWN)
		{
			WPoint ptScreen;
			CPoint pt;
			pt.x=pMsg->pt.x;
			pt.y=pMsg->pt.y;
			this->ScreenToClient(&pt);
			ptScreen.x=pt.x;
			ptScreen.y=pt.y;
			m_pRootContainer->HandleEvent(MOUSE_LBUTTON_DOWN,(int)(&ptScreen),pMsg->lParam);



		}

		
		if(pMsg->message==WM_MOUSEMOVE)
		{
			WPoint ptScreen;
			CPoint pt;
			pt.x=pMsg->pt.x;
			pt.y=pMsg->pt.y;
			this->ScreenToClient(&pt);
			ptScreen.x=pt.x;
			ptScreen.y=pt.y;
			m_pRootContainer->HandleEvent(MOUSE_MOVE,(int)(&ptScreen),pMsg->lParam);

		}
		return CViewWnd::PreTranslateMessage(pMsg);
	}
